package com.yanqu.road.dao.impl.activity.ruishou;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouActionConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouActionTenConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouAreaConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouMonsterConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RuiShouConfigDaoImpl extends TempDao {

    private static String getRuiShouAreaConfigSql = "select * from `t_s_activity_ruishou_area` where `activityId`=? ORDER BY `area` ASC";

    public Map<Integer, RuiShouAreaConfig> getRuiShouAreaConfigMap(int activityId) {
        Map<Integer, RuiShouAreaConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getRuiShouAreaConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RuiShouAreaConfig data = new RuiShouAreaConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setArea(rs.getInt("area"));
                data.setName(rs.getString("name"));
                data.setUnlock(rs.getString("unlock"));
                data.setNum(rs.getString("num"));
                data.setScore(rs.getString("score"));
                result.put(data.getArea(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getRuiShouMonsterConfigSql = "select * from `t_s_activity_ruishou_monster` where `activityId`=? ORDER BY `id` ASC";

    public Map<Integer, RuiShouMonsterConfig> getRuiShouMonsterConfigMap(int activityId) {
        Map<Integer, RuiShouMonsterConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getRuiShouMonsterConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RuiShouMonsterConfig data = new RuiShouMonsterConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setArea(rs.getInt("area"));
                data.setType(rs.getInt("type"));
                data.setName(rs.getString("name"));
                data.setLevel(rs.getInt("level"));
                data.setAward(rs.getString("award"));
                data.setRandomAward(rs.getString("randomAward"));
                data.setNum(rs.getInt("num"));
                data.setWeight(rs.getInt("weight"));
                String leastNum = rs.getString("leastNum");
                if (!StringUtils.isNullOrEmpty(leastNum)) {
                    String[] leastNumList = leastNum.split("\\|");
                    data.setLeastNum1(Integer.parseInt(leastNumList[0]));
                    data.setLeastNum2(Integer.parseInt(leastNumList[1]));
                }
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getRuiShouActionConfigSql = "select * from `t_s_activity_ruishou_action` where `activityId`=? ORDER BY `id` ASC";

    public Map<Integer, RuiShouActionConfig> getRuiShouActionConfigMap(int activityId) {
        Map<Integer, RuiShouActionConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getRuiShouActionConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RuiShouActionConfig data = new RuiShouActionConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                String move = rs.getString("move");
                if (!StringUtils.isNullOrEmpty(move)) {
                    data.setMovePath(StringUtils.stringToIntegerList(move, "\\|"));
                }
                data.setReturnPos(rs.getInt("return"));
                data.setActionData(rs.getString("actionDate"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getRuiShouActionTenConfigSql = "select * from `t_s_activity_ruishou_action_ten` where `activityId`=? ORDER BY `id` ASC";

    public Map<Integer, RuiShouActionTenConfig> getRuiShouActionTenConfigMap(int activityId) {
        Map<Integer, RuiShouActionTenConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getRuiShouActionTenConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RuiShouActionTenConfig data = new RuiShouActionTenConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                String array = rs.getString("array");
                if (!StringUtils.isNullOrEmpty(array)) {
                    data.setActionIdList(StringUtils.stringToIntegerList(array, "#"));
                }
                data.setWeight(rs.getInt("weight"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }



}
